﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Org.LyrcsoftFramework.Cloud.OpenFeign
{
    public interface IHttpClientFactory
    {
        HttpClient CreateClient(string name);

        HttpClient CreateClient(string name, HttpClient httpClient);
    }

    /// <summary>
    /// HttpClient 默认工厂对象
    /// </summary>
    public class DefaultHttpClientFactory : IHttpClientFactory
    {
        private readonly Dictionary<string, HttpClient> _httpClientDictionary = new Dictionary<string, HttpClient>();

        public HttpClient CreateClient(string name)
        {
            if (_httpClientDictionary.ContainsKey(name))
            {
                return _httpClientDictionary[name];
            }
            var httpClient = new HttpClient { };
            _httpClientDictionary[name] = httpClient;
            return httpClient;
        }

        public HttpClient CreateClient(string name, HttpClient httpClient)
        {
            if (_httpClientDictionary.ContainsKey(name))
            {
                return _httpClientDictionary[name];
            }
            _httpClientDictionary[name] = httpClient;
            return httpClient;
        }
    }



}
